<?php

namespace app\validate\web\students;

use app\model\admin\User;
use app\model\Students;
use app\service\ConfServiceFacade;
use laytp\library\Str;
use think\facade\Config;
use think\Validate;

class Login extends Validate
{
    protected $rule = [
        'username'    => 'require',
        'password'    => 'require|checkPassword:',
    ];

    //定义内置方法检验失败后返回的字符
    protected $message = [
        'username.require' => '用户名不能为空',
        'password.require' => '密码不能为空',
    ];


    //自定义密码检验方法
    protected function checkPassword($password, $rule, $data)
    {
        $username = $data['username'];
        $user     = Students::where('phone', '=', $username)->find();
        if (!$user) {
            return '用户未注册';
        }

        $passwordHash = $user->password;
        if (!Str::checkPassword($password, $passwordHash)) {
            return '用户名或密码错误';
        }

        $status = $user->status;
        if ($status == 0) {
            return '用户已被禁用，请联系管理员';
        }

        return true;
    }
}